//
//  UIColor+HWCategory.m
//  OCDome
//
//  Created by 李含文 on 2020/11/9.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "UIColor+HWCategory.h"

@implementation UIColor (HWCategory)


// 默认alpha值为1
+ (UIColor *)hw_color:(NSString *)hexString {
    return [self hw_color:hexString alpha:1.0f];
}
/**
 16进制转颜色
 
 @param hexString 16进制字符串
 @param alpha 透明度0~1
 @return 返回颜色
 */
+ (UIColor *)hw_color:(NSString *)hexString alpha:(CGFloat)alpha {
    //删除字符串中的空格
    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"]) {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6) {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}

+ (UIColor *)hw_gradientColor:(HWColorGradientDirection)direction frame:(CGRect)frame starthex:(NSString *)start endhex:(NSString *)end {
    return [self hw_gradientColor:direction frame:frame startColor:[self hw_color:start] endColor:[self hw_color:end]];
}

+ (UIColor *)hw_gradientColor:(HWColorGradientDirection)direction frame:(CGRect)frame startColor:(UIColor *)start endColor:(UIColor *)end {
    // CAGradientLayer实现渐变
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.frame = frame;
    gradientLayer.colors = @[(__bridge id)start.CGColor,(__bridge id)end.CGColor];
    switch (direction) {
        case HW_LEFT_RIGHT: {
            gradientLayer.startPoint = CGPointMake(0, 0);
            gradientLayer.endPoint = CGPointMake(1, 0);
        } break;
        case HW_TOP_BOTTOM: {
            gradientLayer.startPoint = CGPointMake(0, 0);
            gradientLayer.endPoint = CGPointMake(0, 1);
        } break;
            
        default:
            break;
    }
    UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, NO, [UIScreen mainScreen].scale);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *bgColorImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [UIColor colorWithPatternImage:bgColorImage];
}


+ (UIColor*)hw_randomColor {
    NSInteger aRedValue = arc4random() %255;
    NSInteger aGreenValue = arc4random() %255;
    NSInteger aBlueValue = arc4random() %255;
    UIColor * randColor = [UIColor colorWithRed:aRedValue /255.0f green:aGreenValue /255.0f blue:aBlueValue /255.0f alpha:1.0f];
    return randColor;
}

+ (UIColor *)hw_colorWithRGB:(uint32_t)rgbValue {
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0f
                           green:((rgbValue & 0xFF00) >> 8) / 255.0f
                            blue:(rgbValue & 0xFF) / 255.0f
                           alpha:1];
}

- (uint32_t)hw_rgbValue {
    CGFloat r = 0, g = 0, b = 0, a = 0;
    [self getRed:&r green:&g blue:&b alpha:&a];
    int8_t red = r * 255;
    uint8_t green = g * 255;
    uint8_t blue = b * 255;
    return (red << 16) + (green << 8) + blue;
}
@end

@implementation NSString (HWColorCategory)

- (UIColor *)hw_toColor {
    if (self.length == 0) return UIColor.whiteColor;
    return [UIColor hw_color:self];
}

@end
